/**
 * 
 */
package com.yidin.lsc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yidin.entry.MyDataEntry;
import com.yidin.entry.MyDynamicEntry;
import com.yidin.entry.MyPhotoDetailAdapter;
import com.yidin.entry.MyPhotoDetailAdapter.photoChange;
import com.yidin.entry.LoginInfo;
import com.yidin.entry.MyPhotoDetailEntry;
import com.yidin.entry.PhotoListEntry;
import com.yidin.entry.UserLoadCommentListEntry;
import com.yidin.entry.UserLoadGiftListEntry;
import com.yidin.logic.MainService;
import com.yidin.logic.Task;
import com.yidin.util.NetImageBase;
import com.yidin.util.imageCallback;
import com.yidin.widget.SlowGallery;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 我的照片详细页面
 * @author cola
 *
 */
public class MyPhotoDetail extends BaseActivity implements photoChange{
	private ArrayList<PhotoListParcelable> PhotoListParcelableList;
	private List<UserLoadCommentListEntry> m = null;
	private MyPhotoDetailEntry me;
	private ListView dynamicListView;
	
	private UserLoadCommentListAdapter myDynamicAdapter;
	private SlowGallery slowGallery;
	List<PhotoListEntry> filterArray = new ArrayList<PhotoListEntry>();
	private TextView MemberName;
	//private TextView FilePath;
	//private TextView FileDesc;
	private TextView AddressInfo;
	private TextView DeviceInfo;
	private TextView ReadCount;
	private TextView title;
	private Button PraiseCount;
	private Button CommentCount;
	private ImageView face;
	private NetImageBase netImageBase;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myphotodetail);
		init();
	}
	
	@Override
	public void init() {
		// TODO Auto-generated method stub
		netImageBase = new NetImageBase();
		PhotoListParcelableList = this.getIntent().getParcelableArrayListExtra("list");
		LoadImageFilter();
		
		this.slowGallery = (SlowGallery) findViewById(R.id.galleryFilter);
		this.title = (TextView) findViewById(R.id.title);
//		this.FileDesc = (TextView) 
//				findViewById(R.id.updateIntroduction);
		this.dynamicListView = (ListView) findViewById(R.id.myFriendListView);
		this.AddressInfo = (TextView) findViewById(R.id.pro_addr);
		this.DeviceInfo = (TextView) findViewById(R.id.pro_from);
		this.ReadCount = (TextView) findViewById(R.id.pro_look);
		this.MemberName = (TextView) findViewById(R.id.pro_unname);
		
		this.PraiseCount = (Button) findViewById(R.id.PraiseCount);
		this.PraiseCount .setOnClickListener(PraiseCountOnClickListener);
		this.CommentCount = (Button) findViewById(R.id.CommentCount);
		
		Map obj = new HashMap<String, String>();
		//obj.put("AccountId", lscManager.getInstance().myAccount.name);
		obj.put("ObjectKind", "MemberFile");
		obj.put("ObjectNo", PhotoListParcelableList.get(0).Id);
		obj.put("Id", PhotoListParcelableList.get(0).Id);

		showProgress(this);
		Task task = new Task(Task.TASK_USERPHOTO_DETAIL, obj);
		MainService.AddNewTask(task);
	}

	private OnClickListener PraiseCountOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Map obj = new HashMap<String, String>();
			obj.put("ObjectKind", "MemberFile");
			obj.put("ObjectNo", PhotoListParcelableList.get(0).Id);
			obj.put("AccountId", lscManager.getInstance().myAccount.name);

			showProgress(MyPhotoDetail.this);
			Task task = new Task(Task.TASK_PHOTO_ADDPRAISE, obj);
			MainService.AddNewTask(task);
		}
	};
	
	/**
	 * 加载图片filter
	 */
	private void LoadImageFilter() {
		SlowGallery gallery = (SlowGallery) 
				findViewById(R.id.galleryFilter);
		
//		filterArray.add("1");
//		filterArray.add("2");
//		filterArray.add("3");
		gallery.setAdapter(new MyPhotoDetailAdapter(PhotoListParcelableList, this));
		gallery.setSelection(0);
		gallery.setAnimationDuration(3000);
		gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long id) {

			}
		});
	}
	
	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		this.dismissProgress();
		int task = (Integer) param[0];
		switch (task) {
		case Task.TASK_USERPHOTO_DETAIL: {
			m = (List<UserLoadCommentListEntry>) param[1];
			me = (MyPhotoDetailEntry) param[2];
			if (m == null)
				return;
			myDynamicAdapter = new UserLoadCommentListAdapter(m,this);
			dynamicListView.setAdapter(myDynamicAdapter);

			this.PraiseCount.setText(me.PraiseCount + "人");
//			this.FileDesc.setText(me.FileDesc);
			this.AddressInfo.setText(me.AddressInfo);
			this.DeviceInfo.setText(me.DeviceInfo);
			this.ReadCount.setText(me.ReadCount);
			this.MemberName.setText( me.MemberName);
			this.title.setText(me.MemberName);
			this.CommentCount.setText(me.CommentCount + "人");

			Bitmap bm = null;
			NetImageBase n = new NetImageBase();
			if (me.PhotoUrl != null) {// 请求图标
				bm = n.getLogoBitmap(handler, MyPhotoDetail.this,
						me.PhotoUrl,
						new imageCallback() {
							public void getImage(Bitmap bm) {
								// TODO Auto-generated method stub
								if (bm != null) {
									face.setImageBitmap(bm);
									//getIsSelected().put(i, bm);
								}
							}
						});
				if(bm != null){
					//bm = n.getLocalBitmap(null, MyPhotoDetail.this, me.PhotoUrl, 450, 170);
					//BitmapDrawable bd= new BitmapDrawable(getResources(), bm); 
					face.setImageBitmap(bm);
				}
			}
		
			break;
		}
		case Task.TASK_PHOTO_ADDPRAISE: {
			LoginInfo p = (LoginInfo) param[1];
			if (p == null)
				return;
			if (p.e.equals("0000")) {
				Toast.makeText(this, "操作成功", Toast.LENGTH_LONG).show();
			} else if (p.e.equals(""))
				Toast.makeText(this, "服务器返回失败信息", Toast.LENGTH_LONG).show();
			else
				Toast.makeText(this, p.e, Toast.LENGTH_LONG).show();
		}
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void changePosition(int p) {
		// TODO Auto-generated method stub
		
	}

}
